<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型"></el-input>
    </el-form-item>
    <el-form-item label="短url" prop="slug">
      <el-input v-model="dataForm.slug" placeholder="短url"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="别名" prop="alias">
      <el-input v-model="dataForm.alias" placeholder="别名"></el-input>
    </el-form-item>
    <el-form-item label="封面url" prop="cover">
      <el-input v-model="dataForm.cover" placeholder="封面url"></el-input>
    </el-form-item>
    <el-form-item label="上映年份" prop="year">
      <el-input v-model="dataForm.year" placeholder="上映年份"></el-input>
    </el-form-item>
    <el-form-item label="国家" prop="regions">
      <el-input v-model="dataForm.regions" placeholder="国家"></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="genres">
      <el-input v-model="dataForm.genres" placeholder="类型"></el-input>
    </el-form-item>
    <el-form-item label="语言" prop="languages">
      <el-input v-model="dataForm.languages" placeholder="语言"></el-input>
    </el-form-item>
    <el-form-item label="上映日期" prop="releaseDate">
      <el-input v-model="dataForm.releaseDate" placeholder="上映日期"></el-input>
    </el-form-item>
    <el-form-item label="豆瓣url" prop="officialSite">
      <el-input v-model="dataForm.officialSite" placeholder="豆瓣url"></el-input>
    </el-form-item>
    <el-form-item label="导演" prop="directors">
      <el-input v-model="dataForm.directors" placeholder="导演"></el-input>
    </el-form-item>
    <el-form-item label="演员" prop="actors">
      <el-input v-model="dataForm.actors" placeholder="演员"></el-input>
    </el-form-item>
    <el-form-item label="简介" prop="storyline">
      <el-input v-model="dataForm.storyline" placeholder="简介"></el-input>
    </el-form-item>
    <el-form-item label="时长" prop="mins">
      <el-input v-model="dataForm.mins" placeholder="时长"></el-input>
    </el-form-item>
    <el-form-item label="" prop="recommendTip">
      <el-input v-model="dataForm.recommendTip" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="标签" prop="tags">
      <el-input v-model="dataForm.tags" placeholder="标签"></el-input>
    </el-form-item>
    <el-form-item label="" prop="avgScore">
      <el-input v-model="dataForm.avgScore" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="imdbId">
      <el-input v-model="dataForm.imdbId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="imdbScore">
      <el-input v-model="dataForm.imdbScore" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="imdbVotes">
      <el-input v-model="dataForm.imdbVotes" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="豆瓣id" prop="doubanId">
      <el-input v-model="dataForm.doubanId" placeholder="豆瓣id"></el-input>
    </el-form-item>
    <el-form-item label="豆瓣评分" prop="doubanScore">
      <el-input v-model="dataForm.doubanScore" placeholder="豆瓣评分"></el-input>
    </el-form-item>
    <el-form-item label="评价人数" prop="doubanVotes">
      <el-input v-model="dataForm.doubanVotes" placeholder="评价人数"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updatedAt">
      <el-input v-model="dataForm.updatedAt" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          type: '',
          slug: '',
          name: '',
          alias: '',
          cover: '',
          year: '',
          regions: '',
          genres: '',
          languages: '',
          releaseDate: '',
          officialSite: '',
          directors: '',
          actors: '',
          storyline: '',
          mins: '',
          recommendTip: '',
          tags: '',
          avgScore: '',
          imdbId: '',
          imdbScore: '',
          imdbVotes: '',
          doubanId: '',
          doubanScore: '',
          doubanVotes: '',
          createdAt: '',
          updatedAt: ''
        },
        dataRule: {
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          slug: [
            { required: true, message: '短url不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          alias: [
            { required: true, message: '别名不能为空', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: '封面url不能为空', trigger: 'blur' }
          ],
          year: [
            { required: true, message: '上映年份不能为空', trigger: 'blur' }
          ],
          regions: [
            { required: true, message: '国家不能为空', trigger: 'blur' }
          ],
          genres: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          languages: [
            { required: true, message: '语言不能为空', trigger: 'blur' }
          ],
          releaseDate: [
            { required: true, message: '上映日期不能为空', trigger: 'blur' }
          ],
          officialSite: [
            { required: true, message: '豆瓣url不能为空', trigger: 'blur' }
          ],
          directors: [
            { required: true, message: '导演不能为空', trigger: 'blur' }
          ],
          actors: [
            { required: true, message: '演员不能为空', trigger: 'blur' }
          ],
          storyline: [
            { required: true, message: '简介不能为空', trigger: 'blur' }
          ],
          mins: [
            { required: true, message: '时长不能为空', trigger: 'blur' }
          ],
          recommendTip: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          tags: [
            { required: true, message: '标签不能为空', trigger: 'blur' }
          ],
          avgScore: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          imdbId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          imdbScore: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          imdbVotes: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          doubanId: [
            { required: true, message: '豆瓣id不能为空', trigger: 'blur' }
          ],
          doubanScore: [
            { required: true, message: '豆瓣评分不能为空', trigger: 'blur' }
          ],
          doubanVotes: [
            { required: true, message: '评价人数不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/movies/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.movies.type
                this.dataForm.slug = data.movies.slug
                this.dataForm.name = data.movies.name
                this.dataForm.alias = data.movies.alias
                this.dataForm.cover = data.movies.cover
                this.dataForm.year = data.movies.year
                this.dataForm.regions = data.movies.regions
                this.dataForm.genres = data.movies.genres
                this.dataForm.languages = data.movies.languages
                this.dataForm.releaseDate = data.movies.releaseDate
                this.dataForm.officialSite = data.movies.officialSite
                this.dataForm.directors = data.movies.directors
                this.dataForm.actors = data.movies.actors
                this.dataForm.storyline = data.movies.storyline
                this.dataForm.mins = data.movies.mins
                this.dataForm.recommendTip = data.movies.recommendTip
                this.dataForm.tags = data.movies.tags
                this.dataForm.avgScore = data.movies.avgScore
                this.dataForm.imdbId = data.movies.imdbId
                this.dataForm.imdbScore = data.movies.imdbScore
                this.dataForm.imdbVotes = data.movies.imdbVotes
                this.dataForm.doubanId = data.movies.doubanId
                this.dataForm.doubanScore = data.movies.doubanScore
                this.dataForm.doubanVotes = data.movies.doubanVotes
                this.dataForm.createdAt = data.movies.createdAt
                this.dataForm.updatedAt = data.movies.updatedAt
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/movies/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'slug': this.dataForm.slug,
                'name': this.dataForm.name,
                'alias': this.dataForm.alias,
                'cover': this.dataForm.cover,
                'year': this.dataForm.year,
                'regions': this.dataForm.regions,
                'genres': this.dataForm.genres,
                'languages': this.dataForm.languages,
                'releaseDate': this.dataForm.releaseDate,
                'officialSite': this.dataForm.officialSite,
                'directors': this.dataForm.directors,
                'actors': this.dataForm.actors,
                'storyline': this.dataForm.storyline,
                'mins': this.dataForm.mins,
                'recommendTip': this.dataForm.recommendTip,
                'tags': this.dataForm.tags,
                'avgScore': this.dataForm.avgScore,
                'imdbId': this.dataForm.imdbId,
                'imdbScore': this.dataForm.imdbScore,
                'imdbVotes': this.dataForm.imdbVotes,
                'doubanId': this.dataForm.doubanId,
                'doubanScore': this.dataForm.doubanScore,
                'doubanVotes': this.dataForm.doubanVotes,
                'createdAt': this.dataForm.createdAt,
                'updatedAt': this.dataForm.updatedAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
